package com.hethongviet.job.helpers;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.json.JSONException;
import org.json.JSONObject;

import com.hethongviet.job.AppMain;

public class ServiceHelper1
{
	public static final String URL = "http://quanlytiendo.com/User/";
	public static final String URL_DOWNLOAD_IMAGE = "http://quanlytiendo.com/ImageResource/";

	public static final int LOCATION_ATTR_NONE = 10000;
	public static final String LOCATION_SEPERATE = ",";

	public ServiceHelper1()
	{
	}

	public static String doLogin(String username, String password,
			String activationCode)
	{
		String methodName = "CheckUserName";
		String sendData = "UserName=" + username + "&Password=" + password
				+ "&ActiveCode=" + activationCode;
		return doPost(methodName, sendData);
	}

	@SuppressWarnings("unused")
	private static String getResultStringUseInstreamReader(
			InputStream inputStream)
	{
		InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
		/* Get the string return */
		int charRead = 0;
		String resultStr = "";
		char[] charBuffer = new char[2000];
		try
		{
			while ((charRead = inputStreamReader.read(charBuffer)) > 0)
			{
				resultStr += String.valueOf(charBuffer, 0, charRead);
				charBuffer = new char[2000];
			}
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		AppMain.debug(resultStr);
		return resultStr;
	}

	@SuppressWarnings("unused")
	private static String getResultString(InputStream inputStream)
	{
		String resultStr = "";
		try
		{
			BufferedReader bufferReader = new BufferedReader(new InputStreamReader(inputStream));
			String line;
			StringBuffer response = new StringBuffer();
			while ((line = bufferReader.readLine()) != null)
			{
				response.append(line);
			}
			bufferReader.close();
			resultStr = response.toString();
		} catch (IOException e)
		{
			e.printStackTrace();
		}

		AppMain.debug(resultStr);
		return resultStr;
	}

	public static String doGetListOfProjects(String username, String memberId,
			boolean isManagerJobs)
	{
		String methodName = "GetJobList";
		String sendData = "FK_MemberID=" + memberId + "&UserName=" + username
				+ "&IsManagerJob=" + (isManagerJobs ? "True" : "False");
		return doPost(methodName, sendData);
	}

	public static String doGetListOfAllTasks(String username, String memberId,
			boolean isManagerJobs)
	{
		String methodName = "GetJobItemAll";
		String sendData = "FK_MemberID=" + memberId + "&UserName=" + username
				+ "&IsManagerJob=" + (isManagerJobs ? "True" : "False");
		return doPost(methodName, sendData);
	}

	public static String doGetListOfTasks(String projectId, String username,
			String memberId, boolean isManagerJobs)
	{
		String methodName = "GetJobItemByJobID";
		String sendData = "FK_JobID=" + projectId + "&FK_MemberID=" + memberId
				+ "&UserName=" + username + "&IsManagerJob="
				+ (isManagerJobs ? "True" : "False");
		return doPost(methodName, sendData);
	}

	/**
	 * 
	 * @param memberId
	 * @param imageWorkPath
	 * @param imageData
	 * @param taskId
	 * @param latitude
	 *            (-1 as default)
	 * @param longitude
	 *            (-1 as default)
	 * @return
	 */
	public static String doStartTask(String memberId, String imageWorkPath,
			String imageData, String taskId, double latitude, double longitude)
	{
		String methodName = "StartJobItem";
		JSONObject jsonObject = new JSONObject();
		try
		{
			jsonObject.put("PK_ID", taskId);
			jsonObject.put("ImageWork", getString(imageWorkPath));
			jsonObject.put("ImageWorkByte", getObject(imageData));
			jsonObject.put("FK_MemberAssignID", memberId);
			String latitudeString = "";
			if (latitude != LOCATION_ATTR_NONE)
			{
				latitudeString = latitude + LOCATION_SEPERATE + longitude;
			}
			jsonObject.put("Latitude", latitudeString);

		} catch (JSONException e)
		{
			e.printStackTrace();
		}
		String sendData = "JSON=" + jsonObject.toString();
		return doPost(methodName, sendData);
	}

	public static String doFinishTask(String memberId, String imageWorkPath,
			String imageData, String taskId, double latitude, double longitude)
	{
		String methodName = "FinishJobItem";
		JSONObject jsonObject = new JSONObject();
		try
		{
			jsonObject.put("PK_ID", taskId);
			jsonObject.put("ImageWorkAfter", getString(imageWorkPath));
			jsonObject.put("ImageWorkAfterByte", getObject(imageData));
			jsonObject.put("FK_MemberAssignID", memberId);
			String latitudeAfter = "";
			if (latitude != LOCATION_ATTR_NONE)
			{
				latitudeAfter = latitude + LOCATION_SEPERATE + longitude;
			}
			jsonObject.put("LatitudeAfter", latitudeAfter);

		} catch (JSONException e)
		{
			e.printStackTrace();
		}
		String sendData = "JSON=" + jsonObject.toString();
		return doPost(methodName, sendData);
	}

	public static String doAddComment(String taskId, String commentString,
			String memberId, String imageWorkPath, String imageWorkData,
			String attachmentPath, String attachmentData, double latitude,
			double longitude)
	{
		String methodName = "SaveCommentJobItem";
		JSONObject jsonObject = new JSONObject();
		try
		{
			jsonObject.put("FK_JobItemID", taskId);
			jsonObject.put("Comment", commentString);
			jsonObject.put("FK_MemberID", memberId);
			jsonObject.put("ImageWork", getString(imageWorkPath));
			jsonObject.put("ImageWorkByte", getObject(imageWorkData));
			jsonObject.put("AttachmentName", getString(attachmentPath));
			jsonObject.put("AttachmentNameByte", getObject(attachmentData));
			String latitudeString = "";
			if (latitude != LOCATION_ATTR_NONE)
			{
				latitudeString = latitude + LOCATION_SEPERATE + longitude;
			}
			jsonObject.put("Latitude", latitudeString);
		} catch (JSONException e)
		{
			e.printStackTrace();
		}
		String sendData = "JSON=" + jsonObject.toString();
		return doPost(methodName, sendData);
	}

	public static String doGetListOfComments(String taskId)
	{
		String methodName = "GetCommentJobItem";
		String sendData = "FK_JobItemID=" + taskId;
		return doPost(methodName, sendData);
	}

	public static String doChangePass(String username, String passCurrent,
			String passNew)
	{
		String methodName = "ChangePasswordHTTP";
		String sendData = "OldPassword=" + passCurrent + "&NewPassword="
				+ passNew + "&UserName=" + username;
		return doPost(methodName, sendData);
	}

	private static String doPost(String methodName, String sendData)
	{
		try
		{
			AppMain.debug(URL + methodName + ".aspx?" + sendData);
			// InputStream inputStream =
			String result = new OpenHttpConnection().post(URL + methodName
					+ ".aspx", sendData);

			if (result == null)
			{
				AppMain.debug("Error network! Please check the network!");
			}
			else
			{
				// String result = getResultString(inputStream);
				AppMain.debug("connect ok!!!!!!!!");
				AppMain.debug("Result:\"" + result + "\"");
				return result;
			}

		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return null;
	}

	private static String getString(String stringValue)
	{
		return (stringValue == null) ? "" : stringValue;
	}

	private static Object getObject(Object objectValue)
	{
		return (objectValue == null) ? JSONObject.NULL : objectValue;
		// return (objectValue == null) ? "" : objectValue;
	}

}
